/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __IMPL_RSA_IMPL_H__
#define __IMPL_RSA_IMPL_H__

#include "include_openssl.h"
#include "../tools/head.h"

namespace xos_encrypt
{

    //
    // 定义一下加解密函数指针
    // 
    typedef int ( *f_rsa )( int flen, const unsigned char * from, unsigned char * to, RSA * rsa, int padding );

    class rsa_mgr;

    class rsa_impl : public xos_stl::mem_item< xos::release_impl< rsa_impl >, thread_lock >,
		public i_rsa
    {
    public:
        typedef xos::release_impl< rsa_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        static int get_xos_item_data( ITEM_DATA*& item_ptr );

    public:
        rsa_impl();
        ~rsa_impl();

    protected:
        int padding_step( f_rsa lpFun, RSA * pRSA, char * lpszOutBuf, const char * lpszInBuf, int nInLen );
        int init_data();

    protected:
        rsa_mgr* m_pMgr;
        bool m_bPrivate;

    public:
        int set_mgr( rsa_mgr* pMgr );
        int init();
        int term();

    public:
        // 
        // i_rsa methods
        // 
        // other : 返回长度
        //    -1 : 失败
        int encode( char * lpszOutBuf, const char * lpszInBuf, int nInLen );
        int decode( char * lpszOutBuf, const char * lpszInBuf, int nInLen );

        // encode with buf
        int encode( xos_box::i_buf * pOutBuf, xos_box::i_buf * pInBuf );
        int decode( xos_box::i_buf * pOutBuf, xos_box::i_buf * pInBuf );

        // encode with list
        int encode( xos_box::i_list * pOutList, xos_box::i_list * pInList );
        int decode( xos_box::i_list * pOutList, xos_box::i_list * pInList );

        // 公钥还是私钥端
        int set_type( bool bPrivate );
    };

} // xos_encrypt

#endif // __IMPL_RSA_IMPL_H__
